package com.jia.lidao.others.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.jia.lidao.R;
import com.jia.lidao.others.API;
import com.jia.lidao.others.BaseFragment;
import com.jia.lidao.others.adapter.AreaAdapter;
import com.jia.lidao.others.adapter.CityAdapter;
import com.jia.lidao.others.adapter.ProvinceAdapter;
import com.jia.lidao.others.adapter.QuickAdapter;
import com.jia.lidao.others.enity.ObjectCallBack;
import com.jia.lidao.others.enity.Province;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class CityListFragment extends BaseFragment {
    @BindView(R.id.ib_back)
    ImageView ibBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.rv_province)
    RecyclerView rvProvince;
    @BindView(R.id.rv_city)
    RecyclerView rvCity;
    @BindView(R.id.rv_area)
    RecyclerView rvArea;
    private ProvinceAdapter provinceAdapter;
    private ArrayList<Province.RegionsBean> province;
    private List<Province.RegionsBean> citis;
    private CityAdapter cityAdapter;
    private AreaAdapter areaAdapter;
    private  List<Province.RegionsBean> ares;
    private Province.RegionsBean myProvince;
    private Province.RegionsBean myCity;
    private Province.RegionsBean myArea;
    @Override
    protected int getInflateLayout() {
        return R.layout.city_list_fragment;
    }

    @Override
    protected void setUpView(View view) {
        tvTitle.setText("选择城市");
        tvRight.setText("确定");
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null == myProvince){
                    showToast("请选择省");
                    return;
                }

                if(null == myCity){
                    showToast("请选择市");
                    return;
                }

                if(null == myArea){
                    showToast("请选择区或县");
                    return;
                }

                Intent intent = new Intent();
                intent.putExtra("Province",myProvince);
                intent.putExtra("City",myCity);
                intent.putExtra("Area",myArea);
                getActivity().setResult(Activity.RESULT_OK,intent);
                getActivity().finish();
            }
        });

        provinceAdapter = new ProvinceAdapter();
        province = new ArrayList<>();
        provinceAdapter.setData(province);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvProvince.setLayoutManager(linearLayoutManager);
        rvProvince.setAdapter(provinceAdapter);
        provinceAdapter.setOnItemClickListener(new QuickAdapter.OnItemClickListener<Province.RegionsBean>(){
            @Override
            public void OnItemClick(Province.RegionsBean data, int postion) {


                for(Province.RegionsBean regionsBean :province){
                    if(regionsBean.getId() == data.getId()){
                        regionsBean.isSelector = true;
                    }else {
                        regionsBean.isSelector = false;
                    }
                }
                myProvince = data;
                provinceAdapter.reflashData(province);
                queryDate(data.getId(),2);
            }
        });


        citis = new ArrayList<>();
        cityAdapter = new CityAdapter();
        cityAdapter.setData(citis);
        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(getContext());
        linearLayoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        rvCity.setLayoutManager(linearLayoutManager1);
        rvCity.setAdapter(cityAdapter);

        cityAdapter.setOnItemClickListener(new QuickAdapter.OnItemClickListener<Province.RegionsBean>(){
            @Override
            public void OnItemClick(Province.RegionsBean data, int postion) {
                for(Province.RegionsBean regionsBean :citis){
                    if(regionsBean.getId() == data.getId()){
                        regionsBean.isSelector = true;
                    }else {
                        regionsBean.isSelector = false;
                    }
                }
                myCity = data;
                cityAdapter.reflashData(citis);

                queryDate(data.getId(),3);
            }
        });

        areaAdapter = new AreaAdapter();
        ares = new ArrayList<>();
        areaAdapter.setData(ares);
        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(getContext());
        linearLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
        rvArea.setLayoutManager(linearLayoutManager2);
        rvArea.setAdapter(areaAdapter);
        areaAdapter.setOnItemClickListener(new QuickAdapter.OnItemClickListener<Province.RegionsBean>(){
            @Override
            public void OnItemClick(Province.RegionsBean data, int postion) {
                for(Province.RegionsBean regionsBean :ares){
                    if(regionsBean.getId() == data.getId()){
                        regionsBean.isSelector = true;
                    }else {
                        regionsBean.isSelector = false;
                    }
                }
                myArea = data;
                areaAdapter.reflashData(ares);
            }
        });
        queryDate(0,1);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @OnClick(R.id.ib_back)
    public void onClick() {
        getActivity().finish();
    }


    /**
     * 1 , 2,3
     * @param parent_id
     */
    private void queryDate(int parent_id ,final int level){
        String url = API.MEMBER_ADDRESSES_REGIONS_PARENT+parent_id;

        HashMap<String,String> header = new HashMap<>();
        header.put("Accept","*/*");

        TypeToken<Province> typeToken = new TypeToken<Province>(){};
        OkHttpUtils.get().headers(header).url(url).build().execute(new ObjectCallBack<Province>(typeToken) {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(Province response, int id) {
                if(null != response){
                    if(response.getErr_code() == 0){
                        List<Province.RegionsBean> data=response.getRegions();
                        switch (level){
                            case 1:
                                province.addAll(data);
                                provinceAdapter.reflashData(province);
                                break;
                            case 2:
                                citis.clear();
                                if(null !=data && !data.isEmpty()){
                                    citis.addAll(data);
                                }
                                cityAdapter.reflashData(citis);
                                break;
                            case 3:
                                ares.clear();
                                if(null !=data && !data.isEmpty()){
                                    ares.addAll(data);
                                }
                                areaAdapter.reflashData(ares);
                                break;
                        }

                    }else {
                        showToast(response.getErr_msg());
                    }
                }
            }
        });

    }
}